/*
 * Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://mindorks.com/license/apache-v2
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package org.changs.game.android.di.module;


import android.app.Application;
import android.content.Context;
import android.os.Environment;

import org.changs.game.android.R;
import org.changs.game.android.data.AppDataManager;
import org.changs.game.android.data.DataManager;
import org.changs.game.android.data.db.AppDbHelper;
import org.changs.game.android.data.db.DbHelper;
import org.changs.game.android.data.network.ApiHelper;
import org.changs.game.android.data.network.AppApiHelper;
import org.changs.game.android.data.prefs.AppPreferencesHelper;
import org.changs.game.android.data.prefs.PreferencesHelper;
import org.changs.game.android.di.ApplicationContext;
import org.changs.game.android.di.DatabaseInfo;
import org.changs.game.android.di.DeviceId;
import org.changs.game.android.di.PreferenceInfo;
import org.changs.game.android.utils.AppConstants;
import org.changs.game.android.utils.AppLogger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import uk.co.chrisjenx.calligraphy.CalligraphyConfig;

@Module
public class ApplicationModule {

    private final Application mApplication;
    private String deviceId;

    public ApplicationModule(Application application) {
        mApplication = application;
    }

    @Provides
    @ApplicationContext
    Context provideContext() {
        return mApplication;
    }

    @Provides
    Application provideApplication() {
        return mApplication;
    }

    @Provides
    @DatabaseInfo
    String provideDatabaseName() {
        return AppConstants.DB_NAME;
    }

    @Provides
    @PreferenceInfo
    String providePreferenceName() {
        return AppConstants.PREF_NAME;
    }

    @Provides
    @Singleton
    DataManager provideDataManager(AppDataManager appDataManager) {
        return appDataManager;
    }

    @Provides
    @Singleton
    DbHelper provideDbHelper(AppDbHelper appDbHelper) {
        return appDbHelper;
    }

    @Provides
    @Singleton
    PreferencesHelper providePreferencesHelper(AppPreferencesHelper appPreferencesHelper) {
        return appPreferencesHelper;
    }

    @Provides
    @Singleton
    ApiHelper provideApiHelper(AppApiHelper appApiHelper) {
        return appApiHelper;
    }


    @Provides
    @Singleton
    CalligraphyConfig provideCalligraphyDefaultConfig() {
        return new CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/source-sans-pro/SourceSansPro-Regular.ttf")
                .setFontAttrId(R.attr.fontPath)
                .build();
    }

    @Provides
    @DeviceId
    String provideDeviceId() {
        if (deviceId == null) {
            boolean canWrite = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                    && Environment.getExternalStorageDirectory().canWrite();
            AppLogger.e("provideDeviceId canWrite = " + canWrite);
            String uuId = null;
            File file;
            if (canWrite) {
                file = new File("/sdcard/" + AppConstants.DEVICE_UUID_FILE);
            } else {
                file = new File(mApplication.getFilesDir(), AppConstants.DEVICE_UUID_FILE);
            }
            try {
                FileReader fileReader = new FileReader(file);
                uuId = new BufferedReader(fileReader).readLine();
            } catch (Exception e) {
//                e.printStackTrace();
            }
            if (uuId == null || uuId.trim().length() == 0) {
                uuId = "android-" + UUID.randomUUID().toString().replace("-", "");
                try {
                    FileWriter fileWriter = new FileWriter(file);
                    fileWriter.write(uuId);
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            this.deviceId = uuId;
        }
        return deviceId;
    }
}
